package LinearSearch;

import java.util.Locale;
import java.util.Random;

public class LinearSearch {
    private LinearSearch(){}
    public static <E> int search(E[] data, E target) {
        for (int i = 0; i < data.length; i++)
            if (data[i].equals(target))//data[i] == target是基于基本数据类型，现在是类对象得用equal
                return i;
        return -1;
    }

    public static void main(String[] args) {
        int n=1000000;
        Integer [] data=ArrayGenerator.generateOrderedArray(n);
        long startTime = System.nanoTime();//计时-时间戳ns
        for (int i = 0; i < 100; i++) {
            LinearSearch.search(data,n);
        }
        long endTime = System.nanoTime();
        double time=(endTime-startTime)/ 1000000000.0;
        System.out.println("n=" +n+",100runs:"+time+"s");


       /* String [] daata={"买买买","mss","全额付饭钱"};
        int mss = LinearSearch.search(daata, "mss");
        System.out.println(mss);

        Student[] students={new Student("马学兴"),new Student("alcike"),new Student("Mark")};
        int ll1 = LinearSearch.search(students,new Student("mark") );
        System.out.println(ll1);*/
    }
}
 class Student{
    private String name;
    public Student(String name){
        this.name=name;
    }
    @Override
    public boolean equals(Object student){//自定义equal方法
        if (this==student)
            return true;
        if (student==null)
            return false;
        if (this.getClass()!=student.getClass())
            return false;
       Student another= (Student) student;
       return this.name.toLowerCase().equals(another.name.toLowerCase());
    }
}
